/**
 * @fileOverview Castle游戏实例的debug单元类源文件
 * @author <a href="http://sivory.my.phpcloud.com">Sivory</a>
 * @version 0.0.1
 */

(function() {

	/** @exports _Class as Name.Sivory.Castle.Core.Debug */
	var _Class =

	/**
	 * @class Castle游戏实例的debug单元类。它主要负责记录调试信息。
	 * @param {Name.Sivory.Castle.Core.Game} game 一个游戏实例，参见{@link Name.Sivory.Castle.Core.Game}
	 * @author <a href="http://sivory.my.phpcloud.com">Sivory</a>
	 * @version 0.0.1
	 */
	Name.Sivory.Castle.Core.Debug = function(game) {

			/**
			 * @private
			 * @field
			 * @type Name.Sivory.Castle.Core.Game
			 * @description 调试单元所在的游戏实例
			 * @since 0.0.1
			 */
			this.game = game;

			/**
			 * @private
			 * @field
			 * @type Name.Sivory.Castle.Util.HashTable
			 * @description 游戏调试信息，将显示在界面左上角
			 * @since 0.0.1
			 */
			this.debugDataL = new Name.Sivory.Castle.Util.HashTable();

			/**
			 * @private
			 * @field
			 * @type Name.Sivory.Castle.Util.HashTable
			 * @description 游戏调试信息，将显示在界面右上角
			 * @since 0.0.1
			 */
			this.debugDataR = new Name.Sivory.Castle.Util.HashTable();

			/**
			 * @private
			 * @field
			 * @type Number
			 * @description 用于记录循环体开始的时间
			 * @since 0.0.1
			 */
			this.frameStartTime = 0;

			/**
			 * @private
			 * @field
			 * @type Number
			 * @description 用于记录循环体结束的时间
			 * @since 0.0.1
			 */
			this.frameEndTime = 0;

			/**
			 * @private
			 * @field
			 * @type Number
			 * @description 用于记录循环体开始的时间
			 * @since 0.0.1
			 */
			this.framesPerSecond = 0;

			/**
			 * @private
			 * @field
			 * @type Number
			 * @description 上一个被记录的帧的时间，用于计算FPS
			 * @since 0.0.1
			 */
			this.lastRecordFrameTime = 0;

			/**
			 * @private
			 * @field
			 * @type Number
			 * @description 上一个被记录时帧的计数，用于计算FPS
			 * @since 0.0.1
			 */
			this.lastRecordFrameCount = 0;

		};

	/**
	 * @public
	 * @function
	 * @description 添加调试信息
	 * @param {String} key 调试信息的标识，将显示成 <strong> key : val </strong> 的形式
	 * @param {String} val 调试信息的内容，将显示成 <strong> key : val </strong> 的形式
	 * @param {String} pos 调试信息显示的位置，<strong> "left" </strong> 为显示在左上角，<strong> "right" </strong>为显示在右上角
	 * @since 0.0.1
	 */
	_Class.prototype.addInfo = function(key, val, pos) {
		if(pos === null || pos === undefined) pos = "left";
		var debugData = null;
		if(pos === "left") debugData = this.debugDataL;
		if(pos === "right") debugData = this.debugDataR;
		debugData.put(key, val);
	}

	/**
	 * @public
	 * @function
	 * @description 在一个循环体中开启调试
	 * @since 0.0.1
	 */
	_Class.prototype.start = function() {

		//初始化调试信息
		this.debugDataL.clear();
		this.debugDataR.clear();

		//在调试信息中加入内核版本号
		this.debugDataL.put("Engine Version", this.game.V);

		//在调试信息中加入浏览器信息
		var agents = {
			"C": "Chrome",
			"F": "FireFox",
			"O": "Opera",
			"S": "Safari",
			"I": "Internet Explorer"
		};
		this.debugDataL.put("User Agent", agents[this.game.userAgent]);

		//记录帧开始的时间
		this.frameStartTime = (new Date()).getTime();

		//计算帧率
		var howlong = this.frameStartTime - this.lastRecordFrameTime;
		var howmany = this.game.frameCount - this.lastRecordFrameCount;
		if(howlong > 200) {
			this.framePerSecond = (howmany / howlong * 1000).toFixed(1);
			this.lastRecordFrameTime = this.frameStartTime;
			this.lastRecordFrameCount = this.game.frameCount;
		}
		this.debugDataL.put("FPS", this.framePerSecond);
	}

	/**
	 * @public
	 * @function
	 * @description 在一个循环体中结束调试
	 * @since 0.0.1
	 */
	_Class.prototype.finish = function() {

		//计算单帧的渲染时间
		this.frameEndTime = new Date();
		var runtime = this.frameEndTime - this.frameStartTime;
		this.debugDataL.put("Frame Rendering Time", runtime + " ms");

		//如果开启调试模式的话，则显示调试信息
		if( !! this.game.stage && this.game.config.debug === true) {

			for(var pos in {
				"L": null,
				"R": null
			}) {
				var dubugData = this["debugData" + pos];
				var keys = debugData.keySet();
				if( !! keys.length) for(var i = 0; i < keys.length; i++) {
					this.game.stage.write({
						content: keys[i] + ": " + debugData.get(keys[i]),
						size: 10,
						color: "#000"
					}, 10, 10 + i * 20);
				}
			}
		}
	}

})();